using System;
using Spread.Common.DataStore;
using FarPoint.Win.Spread;
using System.Data;
using FarPoint.Win.Spread.Model;
using System.Collections.Generic;
using FarPoint.Win.Spread.CellType;

namespace Spread.Outline.Features
{
    public partial class GroupAggregation : Spread.Common.Features.Sample.BaseSampleControl
    {
        private AccessDataSource _dataSource;

        public GroupAggregation()
        {
            InitializeComponent();
            Name = "Aggregation and Group Footers";

            _dataSource = new DataService().AccessDataSource;

            InitSpreadSheet(fpSpread.ActiveSheet);
            InitControls();

            fpSpread.Grouped += FpSpread_Grouped;
            fpSpread.Ungrouped += FpSpread_Ungrouped;
        }

        #region Private methods
        private void InitSpreadSheet(SheetView sheet)
        {
            sheet.DataSource = _dataSource.GetDataFrom("fpWine.mdb", "select OrderID, CustomerID, SalesPerson, " +
                "OrderDate, ShippedDate, Freight from Orders Where CustomerID <> -1 " +
                "order by CustomerID ASC, SalesPerson ASC").Tables[0];
            sheet.GroupBarInfo.Visible = true;
            sheet.AllowGroup = true;
            fpSpread.AllowColumnMove = true;
            sheet.GroupFooterVisible = true;

            DataTable customerTable = _dataSource.GetDataFrom("fpWine.mdb", "select ID, CompanyName from Customers")
                .Tables[0];
            List<string> ids = new List<string>();
            List<string> names = new List<string>();
            foreach (DataRow dataRow in customerTable.Rows)
            {
                ids.Add(dataRow[0].ToString());
                names.Add(dataRow[1].ToString());
            }

            ComboBoxCellType comboCell = new ComboBoxCellType();
            comboCell.ItemData = ids.ToArray();
            comboCell.Items = names.ToArray();
            comboCell.EditorValue = EditorValue.ItemData;

            sheet.Columns[1].CellType = comboCell;

            for (int col = 0; col < sheet.ColumnCount; ++col)
                sheet.Columns[col].Width = sheet.GetPreferredColumnWidth(col, PreferredSizeColumnOptions.IncludeAll);

            sheet.SetMultipleRowHeights(0, sheet.RowCount, 20);
        }

        private void InitControls()
        {
            comboAggregationType.DataSource = Enum.GetValues(typeof(AggregationType));
            comboAggregationType.SelectedItem = AggregationType.Sum;
            comboAggregationType.SelectedIndexChanged += ComboAggregationType_SelectedIndexChanged;

            chkShowFooter.CheckedChanged += ChkShowFooter_CheckedChanged;
        }

        private void UpdateFooterAggregation()
        {
            GroupDataModel groupDataModel = (GroupDataModel)fpSpread.ActiveSheet.Models.Data;
            groupDataModel.GroupFooterVisible = chkShowFooter.Checked;
            for (int i = 0; i < groupDataModel.Groups.Count; i++)
            {
                Group g1 = (Group)groupDataModel.Groups[i];
                ((IAggregationSupport)g1.GroupFooter.DataModel).SetCellAggregationType(0, 5,
                    (AggregationType)comboAggregationType.SelectedItem);
                ((IAggregationSupport)g1.GroupFooter.DataModel).SetCellAggregationFormat(0, 5,
                    $"{comboAggregationType.SelectedItem}" + ": {0:F2}");
                fpSpread.ActiveSheet.Models.Data = groupDataModel;
            }
            fpSpread.ActiveSheet.Columns[5].Width = fpSpread.ActiveSheet.Columns[5].GetPreferredWidth();
        }
        #endregion

        #region Event handlers
        private void FpSpread_Ungrouped(object sender, EventArgs e)
        {
            groupBox1.Enabled = false;
        }

        private void FpSpread_Grouped(object sender, EventArgs e)
        {
            groupBox1.Enabled = true;
            UpdateFooterAggregation();
        }

        private void ComboAggregationType_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateFooterAggregation();
        }

        private void ChkShowFooter_CheckedChanged(object sender, EventArgs e)
        {
            comboAggregationType.Enabled = chkShowFooter.Checked;
            (fpSpread.ActiveSheet.Models.Data as GroupDataModel).GroupFooterVisible = chkShowFooter.Checked;
            fpSpread.ActiveSheet.Columns[5].Width = fpSpread.ActiveSheet.Columns[5].GetPreferredWidth();
        }
        #endregion
    }
}


//
//GroupAggregation.Designer.cs
//


namespace Spread.Outline.Features
{
    partial class GroupAggregation
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.comboAggregationType = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.chkShowFooter = new System.Windows.Forms.CheckBox();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.groupBox1);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White;
            this.fpSpread.StatusBarVisible = true;
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.comboAggregationType);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.chkShowFooter);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Enabled = false;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(157, 136);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Footer Settings";
            // 
            // comboAggregationType
            // 
            this.comboAggregationType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.comboAggregationType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboAggregationType.FormattingEnabled = true;
            this.comboAggregationType.Location = new System.Drawing.Point(7, 65);
            this.comboAggregationType.Name = "comboAggregationType";
            this.comboAggregationType.Size = new System.Drawing.Size(121, 21);
            this.comboAggregationType.TabIndex = 2;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(7, 47);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(94, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Aggregation Type:";
            // 
            // chkShowFooter
            // 
            this.chkShowFooter.AutoSize = true;
            this.chkShowFooter.Checked = true;
            this.chkShowFooter.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkShowFooter.Location = new System.Drawing.Point(7, 20);
            this.chkShowFooter.Name = "chkShowFooter";
            this.chkShowFooter.Size = new System.Drawing.Size(118, 17);
            this.chkShowFooter.TabIndex = 0;
            this.chkShowFooter.Text = "Show Group Footer";
            this.chkShowFooter.UseVisualStyleBackColor = true;
            // 
            // GroupAggregation
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "GroupAggregation";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.ComboBox comboAggregationType;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.CheckBox chkShowFooter;
    }
}
